package com.rongc.client.freight.business.carrier.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.base.view.activity.HostActivity;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;
import com.rongc.client.freight.business.carrier.request.api.ConveyanceCancelApi;
import com.rongc.client.freight.business.carrier.view.adapter.RecyclerConveyanceSupplyAdapter;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.view.fragment.SupplyFragment;
import com.rongc.client.freight.business.waybill.request.api.WaybillConfirmedApi;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by Bruce on 2017/7/14.
 */
public class ConveyanceWaybillDetailActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerConveyanceSupplyAdapter modelAdapter;
    List<SupplyBean> datas;

    String waybillId;
    String conveyanceId;
    SupplyBean supplyBean;
    ConveyanceBean conveyanceBean;
    NormalDialog dialog;

    SupplyFragment supplyFragment;

    @OnClick({R.id.btn_submit})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                String number = (String)v.getTag();
                ActivityUtils.callTo(this,number);
                break;
            case R.id.tv_status:
                dialog.show();
                break;
        }
    }

    Response.Listener<JSONObject> respWaybillListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                {
                    supplyBean = new SupplyBean();
                    supplyBean.setStart_ssq(response.optString("goodsStartssq"));
                    supplyBean.setEnd_ssq(response.optString("goodsEndssq"));
                    supplyBean.setDistance(response.optString("distance"));
                    supplyBean.setGoods(response.optString("goods"));
                    supplyBean.setLoadTime(response.optString("goodsLoadtime"));
                    supplyBean.setCreate_time(response.optString("createtime"));
                    supplyBean.setFreight(response.optString("freight"));
                    supplyBean.setDilk(response.optString("goodsDulk"));
                    supplyBean.setCarLength(response.optString("goodsLength"));
                    supplyBean.setCarModels(response.optString("goodsModel"));
                    supplyBean.setId(response.optString("goodsId"));
                    supplyBean.setDilk(response.optString("goodsDulk"));
                    supplyBean.setSj_price(response.optString("sj_price"));

                    conveyanceBean = new ConveyanceBean();
                    conveyanceBean.setStart_ssq(response.optString("trucksStartssq"));
                    conveyanceBean.setEnd_ssq(response.optString("trucksEndssq"));
                    conveyanceBean.setLoadTime(response.optString("trucksLoadtime"));
                    conveyanceBean.setCarLoad(response.optString("trucksCarload"));
                    conveyanceBean.setCarLength(response.optString("trucksLength"));
                    conveyanceBean.setCarModels(response.optString("trucksModel"));
                    conveyanceBean.setId(response.optString("trucksId"));
                    conveyanceBean.setDulk(response.optString("trucksDulk"));
                }
                conveyanceId = conveyanceBean.getId();
                supplyFragment.setData(conveyanceBean);

                datas.clear();
                if(datas!=null){
                    datas.add(supplyBean);
                }
                modelAdapter.notifyDataSetChanged();
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceWaybillDetailActivity.this);
            }
        }
    };

    Response.Listener<NullResult> respCancelListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                dialog.cancel();
                finish();
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceWaybillDetailActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceWaybillDetailActivity.this);
        }
    };


    @Override
    public void initData() {
        WaybillConfirmedApi p = new WaybillConfirmedApi(new WaybillConfirmedApi.WaybillStatusParams(
                waybillId),
                respWaybillListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void initParams() {
        waybillId = getIntent().getStringExtra("waybillId");
        conveyanceId = getIntent().getStringExtra("conveyanceId");
    }

    @Override
    public void initView() {
        initToolbar(R.string.conveyance_waybill_title);
        datas = new ArrayList<>();

        supplyFragment = (SupplyFragment) getSupportFragmentManager().findFragmentByTag("supply");
        supplyFragment.setListener(this);
        modelAdapter = new RecyclerConveyanceSupplyAdapter(this,datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);
        modelAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<SupplyBean>() {
            @Override
            public void onItemClick(View view, int position, SupplyBean data) {
                Bundle bundle = new Bundle();
                if(StringUtils.isNotEmpty(conveyanceId)){
                    bundle.putString("conveyanceId",conveyanceId);
                }
                bundle.putSerializable("data",supplyBean);
                bundle.putString("waybillId",waybillId);
                ActivityUtils.startActivity(ConveyanceWaybillDetailActivity.this,ConveyanceSupplyDetailActivity.class,bundle);

            }
        });

        makeDialog();
    }

    void makeDialog(){
        dialog = new NormalDialog(this);
        dialog.isTitleShow(false)
                .content(getResources().getString(R.string.conveyance_supply_dialog_content))
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //  取消绑定
                        ConveyanceCancelApi p = new ConveyanceCancelApi(new ConveyanceCancelApi.ConveyanceCancelParams(
                                conveyanceId),
                                respCancelListener, errorListener);
                        RequestManager.getInstance().call(p);

                    }
                });
    }

    @Override
    public void onBackPressed() {
        ActivityUtils.startActivity(this, HostActivity.class);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            ActivityUtils.startActivity(this, HostActivity.class);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_conveyance_waybill_detail;
    }

}
